//
//  KLChangePwdViewController.m
//  used
//
//  Created by xalo on 16/7/26.
//  Copyright © 2016年 王凯. All rights reserved.
//

#import "KLChangePwdViewController.h"

@interface KLChangePwdViewController ()
@property (weak, nonatomic) IBOutlet UITextField *oldPwd;
@property (weak, nonatomic) IBOutlet UITextField *confirmPwd;
@property (weak, nonatomic) IBOutlet UITextField *changePwd;

@property (nonatomic, strong)MBProgressHUD *hud;

@end

@implementation KLChangePwdViewController

- (MBProgressHUD *)hud {
    
    if (!_hud) {
        _hud = [[MBProgressHUD alloc]initWithView:self.view];
        [self.view addSubview:_hud];
    }
    return _hud;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}


- (IBAction)clickChangeBtn:(id)sender {
    
    BmobUser *user = [BmobUser getCurrentUser];

    if (![self.changePwd.text isEqualToString:self.confirmPwd.text]) {
        [self showMessage:@"密码不一样, 请重新输入"];
    }else {
        
        __weak KLChangePwdViewController *weakSelf = self;
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"确认修改" message:nil preferredStyle:UIAlertControllerStyleAlert];
 
        UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
           
           [user updateCurrentUserPasswordWithOldPassword:weakSelf.oldPwd.text newPassword:weakSelf.confirmPwd.text block:^(BOOL isSuccessful, NSError *error) {
               if (isSuccessful) {
                   YSLLoginRegisterViewController *loginVc = [[YSLLoginRegisterViewController alloc] init];
                   [self presentViewController:loginVc animated:YES completion:nil];
               } else {
                   [self showMessage:@"原始密码错误!"];
               }
           }];
        }];
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alert addAction:confirm];
        [alert addAction:action1];
        [self presentViewController:alert animated:YES completion:nil];
        
    }
}


- (void)showMessage:(NSString *)message {
    self.hud.labelText = message;
    self.hud.mode = MBProgressHUDModeText;
    [self.hud showAnimated:YES whileExecutingBlock:^{
        sleep(1);
    } completionBlock:^{
        [self.hud removeFromSuperViewOnHide];
        self.hud = nil;
    }];
    
}

@end
